<template>
  <div class="app-container">

    <el-card shadow="always" style="margin: 20px">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectTopicList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课题名称" align="center" prop="topicName" />
      <!-- <el-table-column label="课题状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sci_pjt_status" :value="scope.row.status"/>
        </template>
      </el-table-column> -->
      <el-table-column label="课题负责人" align="center" prop="leader" />
      <el-table-column label="课题承担单位" align="center" prop="undertakingUnit" />
      <el-table-column label="总经费(万元)" align="center" prop="totalFunding" width="100"/>
      <el-table-column label="财政专项经费(万元)" align="center" prop="specialFunding" width="150"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>

    <!-- 添加或修改项目课题对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="课题名称" prop="topicName">
          <el-input v-model="form.topicName" placeholder="请输入课题名称" />
        </el-form-item>
        <!-- <el-form-item label="课题状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sci_pjt_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="课题负责人" prop="leader">
          <el-input v-model="form.leader" placeholder="请输入课题负责人" />
        </el-form-item>
        <el-form-item label="课题承担单位" prop="undertakingUnit">
          <el-input v-model="form.undertakingUnit" placeholder="请输入课题承担单位" />
        </el-form-item>
        <el-form-item label="总经费(万元)" prop="totalFunding">
          <el-input v-model="form.totalFunding" placeholder="请输入总经费" />
        </el-form-item>
        <el-form-item label="财政专项经费(万元)"  prop="specialFunding">
          <el-input v-model="form.specialFunding" placeholder="请输入财政专项经费" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProjectTopic, getProjectTopic, delProjectTopic, addProjectTopic, updateProjectTopic } from "@/api/sci/projectTopic";

export default {
  name: "ProjectTopic",
  dicts: ['sci_pjt_status'],
  props: {
    projectId: {
      type: [String, Number], // 根据实际类型选择
      default: null
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目课题表格数据
      projectTopicList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: this.projectId,
        topicName: null,
        status: null,
        leader: null,
        undertakingUnit: null,
        totalFunding: null,
        specialFunding: null
      },
      // 表单参数
      form: {
        projectId: this.projectId,
      },
      // 表单校验
      rules: {

        topicName: [
          { required: true, message: "课题名称不能为空", trigger: "blur" }
        ],
        leader: [
          { required: true, message: "课题负责人不能为空", trigger: "blur" }
        ],
        undertakingUnit: [
          { required: true, message: "课题承担单位不能为空", trigger: "blur" }
        ],
        totalFunding: [
          { required: true, message: "总经费不能为空", trigger: "blur" }
        ],
        specialFunding: [
          { required: true, message: "财政专项经费不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    if(this.projectId !== null){
      this.getList();
    }else{
      this.loading=false;
    }
  },
  watch: {
    projectId(newVal) {
      // 当 projectId 变化时，更新查询参数并重新获取列表
      if (newVal === null) {
        this.queryParams.projectId = null;
        this.projectTopicList = [];
        this.total = 0;
        return;
      }
      this.queryParams.projectId = newVal;
      this.getList();
    }
  },
  methods: {
    /** 查询项目课题列表 */
    getList() {
      this.loading = true;
      listProjectTopic(this.queryParams).then(response => {
        this.projectTopicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectId: null,
        topicName: null,
        progress: null,
        status: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null,
        leader: null,
        isDeleted: null,
        undertakingUnit: null,
        totalFunding: null,
        specialFunding: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目课题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProjectTopic(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目课题";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.projectId = this.projectId;
          if (this.form.id != null) {
            updateProjectTopic(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProjectTopic(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目课题编号为"' + ids + '"的数据项？').then(function() {
        return delProjectTopic(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/projectTopic/export', {
        ...this.queryParams
      }, `projectTopic_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
